package com.qf.homeworkssm.controller;


import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Controller
public class DownLoaderConller {

    @Value("{file.updload.dir}")
    private String uploaddir;

    //1.
    @GetMapping("/download")
    public void downLoad(String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.根据文件名找到文件，把文件内容写回response的对象输出流中
//        String realPath = request.getServletContext().getRealPath("aa");

        String finalfielPath ="D:\\upload"+ File.separator + filename;

        //2.设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+filename);

        IOUtils.copy(new FileInputStream(finalfielPath),response.getOutputStream());

    }



}
